//
//  ViewController.m
//  sign up
//
//  Created by Sizhu Ba on 2017/8/28.
//  Copyright © 2017年 Sizhu Ba. All rights reserved.
//
#define kWidthOfScreen [UIScreen mainScreen].bounds.size.width
#define kHeightOfScreen [UIScreen mainScreen].bounds.size.height

#import "ViewController.h"
#import "Masonry.h"
#import "MBProgressHUD+Ex.h"

@interface ViewController ()

@property (nonatomic, assign) CGFloat viewHeight;
@property (nonatomic, strong) UITextField *phoneNumber;
@property (nonatomic, strong) UILabel *phoneL;//弃用
@property (nonatomic, strong) UIView *line;

@property (nonatomic, strong) UITextField *passWord;
@property (nonatomic, strong) UILabel *passWordL;//弃用
@property (nonatomic, strong) UIView *line2;

@property (nonatomic, strong) UIButton *signBt;

@property (nonatomic, strong) UIButton *see;

@property (nonatomic, strong) UILabel *registerLB;
@property (nonatomic, strong) UILabel *forgetPassWordLB;

@property (nonatomic, strong) UIImageView *pwdIm;
@property (nonatomic, strong) UIImageView *userNameIm;

@end

@implementation ViewController
-(void)loadView{
    [self setView];//设置界面
    
    
  
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.see addTarget:self action:@selector(canSeePassword) forControlEvents:UIControlEventTouchUpInside];
    [self.phoneNumber addTarget:self action:@selector(canlogin) forControlEvents:UIControlEventEditingChanged];
    [self.passWord addTarget:self action:@selector(canlogin) forControlEvents:UIControlEventEditingChanged];
    [self.signBt addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
}
/**
 * 密码是否可见
 */
-(void)canSeePassword {
    if (self.passWord.secureTextEntry){
       
        [self.see setBackgroundImage:[UIImage imageNamed:@"see"] forState:UIControlStateNormal];
        [self.see setBackgroundImage:[UIImage imageNamed:@"see"] forState:UIControlStateHighlighted];
        self.passWord.secureTextEntry = NO;
        
    }else {
        
        [self.see setBackgroundImage:[UIImage imageNamed:@"see_close"] forState:UIControlStateNormal];
        [self.see setBackgroundImage:[UIImage imageNamed:@"see_close"] forState:UIControlStateHighlighted];
        self.passWord.secureTextEntry = YES;
    }
    
}


#pragma mark - 封装的方法
//界面设置
-(void)setView {
    self.viewHeight = kWidthOfScreen < 374.0 ? 150.0 : 200.0; //设置屏幕距离
    //背景
    self.view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen)];
    self.view.backgroundColor = [UIColor colorWithRed:87/255.0 green:229/255.0 blue:237/255.0 alpha:1]; //设置背景为天蓝色
    self.view.backgroundColor = [UIColor whiteColor];
    
    //账号
  //  [self.view addSubview:self.phoneL];
    [self.view addSubview:self.phoneNumber];
    [self.view addSubview:self.line];
    [self.view addSubview:self.userNameIm];
    
    //密码
    [self.view addSubview:self.passWord];
    //[self.view addSubview:self.passWordL];
    [self.view addSubview:self.line2];
    [self.view addSubview:self.pwdIm];
    
    //登录
    [self.view addSubview:self.signBt];
    
    //注册
    [self.view addSubview:self.registerLB];
    
    //忘记密码
    [self.view addSubview:self.forgetPassWordLB];
    [self.view addSubview:self.see];
    
    
    //设置约束
    [self setManory];
    
}

/**
 * 设置约束
 */
-(void)setManory {
        __weak typeof (self) weakSelf = self; //避免循环引用
    [self.see mas_makeConstraints:^(MASConstraintMaker *make) {//眼睛
        make.top.equalTo(weakSelf.passWord.mas_top);//.with.offset(10);
        make.right.equalTo(weakSelf.view.mas_right).with.offset(-20);
        make.height.equalTo(weakSelf.passWord.mas_height);
        make.width.mas_equalTo(@20);
    }];
    
    [self.pwdIm mas_makeConstraints:^(MASConstraintMaker *make) {//密码图标
        make.top.equalTo(weakSelf.passWord.mas_top);//.with.offset(10);
        make.left.equalTo(weakSelf.view.mas_left).with.offset(20);
        make.height.equalTo(weakSelf.passWord.mas_height);
        make.width.mas_equalTo(@20);
    }];
    
    [self.userNameIm mas_makeConstraints:^(MASConstraintMaker *make) {//账号图标
        make.top.equalTo(weakSelf.phoneNumber.mas_top);//.with.offset(10);
        make.left.equalTo(weakSelf.view.mas_left).with.offset(20);
        make.height.equalTo(weakSelf.phoneNumber.mas_height);
        make.width.mas_equalTo(@20);
    }];
 
}
//判断能否登录
-(void)canlogin{
    
    self.signBt.enabled = self.phoneNumber.text.length>0&&self.passWord.text.length>0;
    if (self.signBt.isEnabled) {
        self.signBt.backgroundColor = [UIColor redColor];
        [self.signBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//设置更改文字颜色为白色
        
    }else {
        self.signBt.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1];//[UIColor redColor];
        [self.signBt setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];//设置文字颜色
    }
    
}
#pragma mark - 点击事件
//新用户注册selecter
-(void) registerLabelTouchUpInside:(UITapGestureRecognizer *)recognizer{
    
    UILabel *label=(UILabel*)recognizer.view;
    
    NSLog(@"%@被点击了",label.text);
    
    
}

//忘记密码selecter
-(void) forgetpsLabelTouchUpInside:(UITapGestureRecognizer *)recognizer{
    
    UILabel *label=(UILabel*)recognizer.view;
    
    NSLog(@"%@被点击了",label.text);
    
}

/**
 *登录操作
 */
-(void)login{
    NSLog(@"被点击了");
    [MBProgressHUD showMessage:@"正在登录" toView:self.view];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view];
        });
        
        
        if([self.phoneNumber.text isEqualToString:@"1"]&&[self.passWord.text isEqualToString:@"1"]){
            
            
            
        }else{
            [MBProgressHUD showError:@"登录失败"];
        }
        
    });
    
}

#pragma mark - 懒加载
//输入的电话号码
-(UITextField *)phoneNumber {
    if (_phoneNumber  == nil) {
        _phoneNumber = [[UITextField alloc]initWithFrame:CGRectMake(70, self.viewHeight, kWidthOfScreen-90, kHeightOfScreen/33)];
        //_phoneNumber.backgroundColor = [UIColor orangeColor];
        _phoneNumber.borderStyle =  UITextBorderStyleNone ;
        _phoneNumber.placeholder = @"手机号";
        //_phoneNumber.secureTextEntry = YES; //密码格式
    }
    return _phoneNumber;
}
//账号lable
-(UILabel *)phoneL {
    if (_phoneL == nil) {
        _phoneL = [[UILabel alloc]initWithFrame:CGRectMake(20, self.viewHeight, 40, kHeightOfScreen/33)];
        _phoneL.text = @"Username";
        _phoneL.hidden = YES;
    }
    return  _phoneL;
}
//下划线1
- (UIView *)line {
    if (_line == nil ) {
        _line = [[UIView alloc]initWithFrame:CGRectMake(18, self.viewHeight+kHeightOfScreen/33+kHeightOfScreen/33/3, kWidthOfScreen-36, 1)];
        _line.backgroundColor = [UIColor lightGrayColor];
    }
    return _line;
}


//密码lable
-(UILabel *)passWordL {
    if (_passWordL == nil) {
        _passWordL = [[UILabel alloc]initWithFrame:CGRectMake(20, self.viewHeight+kHeightOfScreen/33+kHeightOfScreen/33/3+20, 40, kHeightOfScreen/33)];
        
        _passWordL.text = @"Password";
        _passWordL.hidden = YES;
    }
    return  _passWordL;
}
//输入的密码
-(UITextField *)passWord {
    if (_passWord  == nil) {
        _passWord = [[UITextField alloc]initWithFrame:CGRectMake(70, self.viewHeight+kHeightOfScreen/33+kHeightOfScreen/33/3+20, kWidthOfScreen-90, kHeightOfScreen/33)];
        //_phoneNumber.backgroundColor = [UIColor orangeColor];
        _passWord.borderStyle =  UITextBorderStyleNone ;
        _passWord.placeholder = @"请输入密码";
        _passWord.secureTextEntry = YES; //密码格式
    }
    return _passWord;
}

//下划线2
- (UIView *)line2 {
    if (_line2 == nil ) {
        _line2 = [[UIView alloc]initWithFrame:CGRectMake(18, self.viewHeight+kHeightOfScreen/33*2+kHeightOfScreen/33/3+20+kHeightOfScreen/33/3, kWidthOfScreen-36, 1)];
        _line2.backgroundColor = [UIColor lightGrayColor];
    }
    return _line2;
}


//登录按钮
-(UIButton *)signBt {
    if (_signBt == nil) {
        _signBt = [[UIButton alloc]initWithFrame:CGRectMake(18, self.viewHeight+kHeightOfScreen/33*2+kHeightOfScreen/33/3+20+kHeightOfScreen/33/3 + 40, kWidthOfScreen-36, 50)];
        _signBt.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1];//[UIColor redColor];
        [_signBt setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];//设置文字颜色
        [_signBt.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
        [_signBt setTitle:@"登       录" forState:UIControlStateNormal];
        _signBt.enabled=NO;//设置不可点击
    }
    return _signBt;
}
//密码是否可见
-(UIButton *)see {
    if (_see == nil) {
        _see = [[UIButton alloc]initWithFrame:CGRectMake(18, self.viewHeight+kHeightOfScreen/33*2+kHeightOfScreen/33/3+20+kHeightOfScreen/33/3 + 40, kWidthOfScreen-36, 50)];
        
        
        [_see setBackgroundImage:[UIImage imageNamed:@"see_close"] forState:UIControlStateNormal];
        [_see setBackgroundImage:[UIImage imageNamed:@"see_close"] forState:UIControlStateHighlighted];
        [_see.layer setCornerRadius:5.0]; //设置矩形四个圆角半径

    }
    return _see;
}
//新用户注册lable
-(UILabel *)registerLB {
    if (_registerLB == nil) {
        _registerLB = [[UILabel alloc]initWithFrame:CGRectMake(20, self.viewHeight+kHeightOfScreen/33*2+kHeightOfScreen/33/3+20+kHeightOfScreen/33/3 + 40+50+10, 90, kHeightOfScreen/33)];
        _registerLB.font = [UIFont systemFontOfSize:14.0];
        _registerLB.text = @"新用户注册";
        _registerLB.userInteractionEnabled=YES;
        UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(registerLabelTouchUpInside:)];
        
        [_registerLB addGestureRecognizer:labelTapGestureRecognizer];
    }
    return  _registerLB;
}
//密码lable
-(UILabel *)forgetPassWordLB {
    if (_forgetPassWordLB == nil) {
        _forgetPassWordLB = [[UILabel alloc]initWithFrame:CGRectMake(kWidthOfScreen-20-70, self.viewHeight+kHeightOfScreen/33*2+kHeightOfScreen/33/3+20+kHeightOfScreen/33/3 + 40+50+10, 70, kHeightOfScreen/33)];
        _forgetPassWordLB.font = [UIFont systemFontOfSize:14.0];
        _forgetPassWordLB.text = @"忘记密码";
        _forgetPassWordLB.textAlignment = NSTextAlignmentRight;
        _forgetPassWordLB.userInteractionEnabled=YES;
        UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(forgetpsLabelTouchUpInside:)];
        
        [_forgetPassWordLB addGestureRecognizer:labelTapGestureRecognizer];
    }
    return  _forgetPassWordLB;
}
/**
 * 密码图标
 */
-(UIImageView *)pwdIm {
    if (_pwdIm == nil) {
        _pwdIm = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pwd"]];
        _pwdIm.frame = CGRectMake(0, 0, 20, 20);
    }
    return _pwdIm;
}
/**
 * 账号图标
 */
-(UIImageView *)userNameIm {
    if (_userNameIm == nil) {
        _userNameIm = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"account"]];
        _userNameIm.frame = CGRectMake(0, 0, 20, 20);
    }
    return _userNameIm;
}
@end
